#include "baterry.h"
#include <QDebug>

Baterry::Baterry(QWidget *parent) : QWidget(parent)
{
    step = 0.5;
    borderWidth = 5;
    borderRadius = 8;
    bgRadius = 5;
    headRadius = 3;
    alarmValue = 30;
    currentValue = 0;
    value = 0;
    isForward = false;

    borderColorStart = QColor(100, 100, 100);
    borderColorEnd = QColor(80, 80, 80);
    alarmColorStart = QColor(250, 118, 113);
    alarmColorEnd = QColor(204, 38, 38);
    normalColorStart = QColor(50, 205, 51);
    normalColorEnd = QColor(60, 179, 133);

    timer = new QTimer(this);
    timer->setInterval(1);
    connect(timer, &QTimer::timeout, this, &Baterry::updateValue);
}


void Baterry::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    drawBorder(&painter);
    drawBg(&painter);
    drawHead(&painter);
}

void Baterry::drawBorder(QPainter *painter)
{
    painter->save();

    double headWidth = width() / 15;
    double batteryWidth = width() - headWidth;

    QPointF topLeft(borderWidth, borderWidth);
    QPointF bottomRight(batteryWidth, height()-borderWidth);
    batteryRect = QRectF(topLeft, bottomRight);

    painter->setPen(QPen(borderColorStart, borderWidth));
    painter->setBrush(Qt::NoBrush);
    painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);

    painter->restore();
}

void Baterry::drawBg(QPainter *painter)
{
    if(value <= 0)
        return ;

    painter->save();

    QLinearGradient batteryGradient(QPointF(0,0), QPointF(0, height()));
    if(value <= alarmValue)
    {
        batteryGradient.setColorAt(0.0, alarmColorStart);
        batteryGradient.setColorAt(1.0, alarmColorEnd);
    }
    else
    {
        batteryGradient.setColorAt(0.0, normalColorStart);
        batteryGradient.setColorAt(1.0, normalColorEnd);
    }

    int margin = qMin(width(), height()) / 20;
    double unit = (batteryRect.width() - margin*2) / 100;
    double width = currentValue * unit;
    QPoint topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y()+margin);
    QPoint bottomRight(width+margin+borderWidth, batteryRect.bottomRight().y()-margin);
    QRectF rect(topLeft, bottomRight);

    painter->setPen(Qt::NoPen);
    painter->setBrush(batteryGradient);
    painter->drawRoundedRect(rect, bgRadius, bgRadius);

    painter->restore();
}

void Baterry::drawHead(QPainter *painter)
{
    painter->save();

    QPointF headRectTopLeft(batteryRect.topRight().x(), height()/3);
    QPointF headRectBottomTopRight(width(), height()-height()/3);
    QRectF headRect(headRectTopLeft, headRectBottomTopRight);

    QLinearGradient headRectGradient(headRect.topLeft(), headRect.bottomLeft());
    headRectGradient.setColorAt(0.0, borderColorStart);
    headRectGradient.setColorAt(1.0, borderColorEnd);

    painter->setPen(Qt::NoPen);
    painter->setBrush(headRectGradient);
    painter->drawRoundedRect(headRect, headRadius, headRadius);

    painter->restore();
}

void Baterry::updateValue()
{
    if(isForward)
    {
        currentValue -= step;
        if(currentValue <= value)
        {
            currentValue = value;
            timer->stop();
        }
    }
    else
    {
        currentValue += step;
        if(currentValue >= value)
        {
            currentValue = value;
            timer->stop();
        }
    }
    update();
}

void Baterry::setValue(int value)
{
    if(this->value == value)
        return ;

    if(value<=0)
        value = 0;
    else if(value>=100)
        value = 100;
    this->value = value;

    if(this->value > currentValue)
    {
        isForward = false;
    }
    else if(value < currentValue)
        isForward = true;
    else
    {
        update();
        return ;
    }
    update();

    timer->start();
}





